package com.wtwd.wtwddevice.deviceserver.handler.processimpl;

import com.wtwd.device.annotation.MsgType;
import com.wtwd.device.constant.MsgTypeEnum;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.protocol.WTProtocol;
import com.wtwd.wtwddevice.deviceserver.handler.MsgHandlerFactory;
import com.wtwd.wtwddevice.deviceserver.handler.ProcessMessageHandler;
import com.wtwd.wtwddevice.deviceserver.model.DeviceSession;
import com.wtwd.wtwddevice.deviceserver.netty.LocalSessions;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@MsgType(MsgTypeEnum.GENERAL_CMD)
public class GeneralCmdHandler implements ProcessMessageHandler {
    @Override
    public WTProtocol processMsg(WTProtocol wtProtocol, Channel channel) {
        DeviceSession deviceLoginSession = LocalSessions.getDeviceLoginSession(wtProtocol.getImei());
        if (deviceLoginSession == null||deviceLoginSession.getChannel()!=channel) {
            //设备未登录
            log.error("设备管通讯滚到已经切换或者设备未登录异常{}-{}", wtProtocol.toString());
            return null;
        }
        MsgHandlerFactory.getMqSendMessage()
                .push(
                        MessageBase.builder()
                                .deviceKey(wtProtocol.getImei())
                                .wtProtocol(wtProtocol)
                                .build());
        return null;
    }
}
